package com.lnking.lnkingplace.Util;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author sn
 * @date 2019/1/23
 * @description TODO
 */
public class FileUtils {

    public static boolean writeStrToFile(String str, String filePath, boolean append) {
        try{
            File file =new File(filePath);
            if(!file.exists()){
                file.createNewFile();
            }
            //true = append file
            OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(filePath),"UTF-8");
            out.write(str);
            out.close();
        }catch(IOException e){
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public static byte[] file2byte(File tradeFile){
        byte[] buffer = null;
        try
        {
            FileInputStream fis = new FileInputStream(tradeFile);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] b = new byte[1024];
            int n;
            while ((n = fis.read(b)) != -1)
            {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
        }catch (FileNotFoundException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }
        return buffer;
    }

    public static void byte2file(byte[] byteArray, String targetPath) {
        InputStream in = new ByteArrayInputStream(byteArray);
        File file = new File(targetPath);
        String path = "";
        if(targetPath.lastIndexOf("/") < 0) {
            path = targetPath.substring(0, targetPath.lastIndexOf("\\"));
        }
        else {
            path = targetPath.substring(0, targetPath.lastIndexOf("/"));
        }
        if (!file.exists()) {
            new File(path).mkdir();
        }
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(file);
            int len = 0;
            byte[] buf = new byte[1024];
            while ((len = in.read(buf)) != -1) {
                fos.write(buf, 0, len);
            }
            fos.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != fos) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static List<String> getFiles(String filePath) {
        List<String> resultFiles = new ArrayList<>();
        File file = new File(filePath);
        if (!file.isDirectory()) {
            resultFiles.add(file.getAbsolutePath());
        } else if (file.isDirectory()) {
            File[] tmpList = file.listFiles();
            for (int i = 0; i < tmpList.length; i++) {
                if (!tmpList[i].isDirectory()) {
                    resultFiles.add(tmpList[i].getAbsolutePath());
                } else if (tmpList[i].isDirectory()) {
                    resultFiles.addAll(getFiles(tmpList[i].getAbsolutePath()));                    }
            }

        }
        return resultFiles;
    }
}
